package com.supercwn.player.app.manager.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.LinearLayout;

import com.supercwn.player.R;
import com.supercwn.player.app.manager.adapter.CleanFileManagerAdapter;
import com.supercwn.player.app.manager.bean.StorageBean;
import com.supercwn.player.app.manager.util.CleanFileManager;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * 项目名称：SuperPlayer-master
 * 类描述：
 * 创建人：TianGenhu
 * 创建时间：2017/12/8 14:19
 * 修改人：TianGenhu
 * 修改时间：2017/12/8 14:19
 * 修改备注：
 */

public class CleanFileManagerActivity extends Activity {

    private String TAG = "TGH";
    private List<StorageBean> files;
    private CleanFileManagerAdapter mAdapter;
    private RecyclerView gRecyclerView;
    private LinearLayoutManager mLayoutManager;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            File externalStorageDirectory = Environment.getExternalStorageDirectory();//获取外部存储目录即 SDCard
            files = CleanFileManager.listLinkedFiles(externalStorageDirectory + "/");

            Log.i(TAG, "文件的个数  ：" + files.size());
            //进行排序
            Collections.sort(files, new Comparator<StorageBean>() {
                @Override
                public int compare(StorageBean o1, StorageBean o2) {
                    if (o1.getSize() > o2.getSize()) {
                        return -1;
                    } else if (o1.getSize() < o2.getSize()) {
                        return 1;
                    } else if (o1.getSize() == o2.getSize()) {
                        return 0;
                    }
                    return 0;
                }
            });

            //小于1兆的不要
            List<StorageBean> fileCompareThesize = new ArrayList<>();
            for (int i = 0; i < files.size(); i++) {
                StorageBean storageBean = files.get(i);
//                if (storageBean.getSize() > 1024 * 1024) {
                if (storageBean.getSize() > 100) {
                    fileCompareThesize.add(storageBean);
                }
            }

            Log.i(TAG, "过滤后的文件个数  ：" + fileCompareThesize.size());
            set(fileCompareThesize);
        }
    };


    private void set(final List<StorageBean> fileCompareThesize) {
        mAdapter = new CleanFileManagerAdapter(CleanFileManagerActivity.this, fileCompareThesize);
        gRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new CleanFileManagerAdapter.OnItemClicks() {
            @Override
            public void onItemClick(int position, LinearLayout line) {
                Log.i(TAG, "点击了Item " + position);
                StorageBean storageBean = fileCompareThesize.get(position);
                String url = storageBean.getUrl();
                CleanFileManager.deleteFolderFile(url, true);
                fileCompareThesize.remove(position);
                mAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_clean_file_manager_activity);
        initView();
    }

    private void initView() {
        gRecyclerView = (RecyclerView) findViewById(R.id.gRecyclerView);
        mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        gRecyclerView.setLayoutManager(mLayoutManager);
        mHandler.sendEmptyMessage(0);
    }


}
